package com.qualiti.javaweb.conversores;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.ConverterException;
import javax.faces.convert.FacesConverter;

import com.qualiti.javaweb.modelo.CPF;

@FacesConverter(value="converterCPF")
public class ConverterCPF implements Converter{

	@Override
	public Object getAsObject(FacesContext arg0, UIComponent arg1, String arg2)
			throws ConverterException {
		CPF cpf = new CPF();
		cpf.setNumero(arg2.substring(0, arg2.length() - 1));
		cpf.setDigitoVerificador(arg2.substring(arg2.length() - 1));
		return cpf;
	}

	@Override
	public String getAsString(FacesContext arg0, UIComponent arg1, Object arg2)
			throws ConverterException {
		if (arg2 == null) {
			return null;
		}
		return ((CPF) arg2).getNumero() + ((CPF) arg2).getDigitoVerificador();
	}


	
}
